package com.cloud.jucTest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 注意区分每一个线程自己new对象的情况
 */
public class WebCurrentTest {
    private static Map<String, Integer> map = new ConcurrentHashMap<String, Integer>();

    private static class Task {
        Integer i;

        public Task(Integer i) {
            this.i = i;
        }

        public void doBiz() {
            Thread.currentThread().setName("Thread" + i);
            map.put("key", i);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " get value is " + map.get("key")) ;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 100; ++i) {
            Task task = new Task(i);
            executorService.execute(() -> {
                task.doBiz();
            });
        }
        Thread.sleep(5000);
        executorService.shutdown();

    }
}
